<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	$GLOBALS['OUTPUT']->master_section = 'management';
	$GLOBALS['OUTPUT']->minor_section = $GLOBALS['LANG']->lang_admin->managerooms;
	$GLOBALS['OUTPUT']->cpmenu = $menus[$GLOBALS['OUTPUT']->master_section];
	
	$del = x7in::read('del','i');
	if(!empty($del)){
		$test = new lib_room($del);
		if($test->is_valid()){
			// Ensure that this group is empty
			if(count($test->get_online_users()) != 0){
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->roomnotempty;
			}else{
				$test->remove();
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->roomdeleted;
			}
		}
	}
	
	// Construct the room creation form
	$form = new form();
	
	// Default default form values (if no other defaults are submitted)
	$form->add_default('max_users','15');
	
	// Check to see if the form has been submitted
	$name = $form->get_add_default('roomname');
	
	if(!empty($name)){
		
		// Load submitted values and insert them back into the form (in case of an error)
		$topic = $form->get_add_default('topic');
		$greeting = $form->get_add_default('greeting');
		$max_users = $form->get_add_default('max_users','i');
		
		// Try to create the new room
		if(is_object($new_room = lib_room_extended::create_room($name,$topic,$greeting,$max_users,!$delete_automatically,$ses->user))){
			// New room created successfully
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->roomcreated;
		}else{
			// An error occured, in this case parsing is easy because the roomname is the only thing that can generate an error
			$form->add_error('roomname',$GLOBALS['LANG']->lang_roomcp->$new_room);
		}
		
	}
	
	$form->add_field('text','roomname',$GLOBALS['LANG']->lang_roomcp->roomname);
	$form->add_field('text','topic',$GLOBALS['LANG']->lang_roomcp->topic,$GLOBALS['LANG']->lang_roomcp->topic_desc);
	$form->add_field('text','greeting',$GLOBALS['LANG']->lang_roomcp->greeting,$GLOBALS['LANG']->lang_roomcp->greeting_desc);
	$form->add_field('text','max_users',$GLOBALS['LANG']->lang_roomcp->max_users);
	
	$riterator = $GLOBALS['DATABASE']->get('rooms');
	$rooms = array();
	while($rdata = $riterator->get_next()){
		$room = new lib_room($rdata['rid']);
		if(!$room->is_valid()) continue;
		$rooms[$room->data_cache['name']] = $rid;
	}
		
	// Output the form
	$GLOBALS['OUTPUT']->form = $form->get_fields();
	$GLOBALS['OUTPUT']->rooms = $rooms;
	
	echo tpl_managerooms::managerooms();
?>